<template>
  <div class='home-page'>
    <Toptab :checkedNum="2"></Toptab>
    <div class="main">
      <div class="header">
        <div class="headeritem" @click="changeCheck(0)"
        :class="{actitem:actNum===0}">全部消息</div>
        <div class="headeritem" @click="changeCheck(1)"
        :class="{actitem:actNum===1}">关于我的</div>
      </div>
      <div class="body">
        <allmessage v-show="actNum===0" ref="allMsg"></allmessage>
        <mymessage v-show="actNum===1" ref="myMsg"></mymessage>
        <div class="usercard">
          <!-- 用户信息 -->
          <div class="user">
            <avatar class="avatar" v-if="profile.avatar!==null" :url="profile.avatar"></avatar>
            <avatar class="avatar" v-else></avatar>
            <div class="userinfo">
              <!-- 用户名 -->
              <div class="username">
                昵称：<span>{{ profile.username }}</span>
              </div>
              <!-- 正在背诵的词表 -->
              <div class="useremail">
                <template v-if="profile.lastword">
                  正在背：<span>{{ profile.lastword }}</span>
                </template>
                <template v-else>
                  暂未开始背诵
                </template>
              </div>
            </div>
          </div>

          <div class="submit">
            <div class="button" @click="startComment()">准备发言</div>
          </div>
        </div>
      </div>
    </div>
  </div>

  <!-- 发言框 -->
  <Dialog
  :show= showDialog
  @confirm="confirm"
  @cancel="cancel"
  :showCancel="true"
  :title="'编辑评论'" :confirmText="`发布`" :cancelText="`关闭`">
  <div class="emjoiBox" v-show="EmjoiChoice">
    <span v-show="emjoyKind===0" class="ps">(ps:一个emjoi占俩个字符哦)</span>
    <!-- 经典小黄豆表情包 -->
    <div class="emjoiList" v-show="emjoyKind===0">
      <div v-for="(item,index) in emjoyjson"
      @click="addEmjoy(item)"
      :key="index"
      class="emjoi-item">
        {{ item}}
      </div>
    </div>
    <!-- 喜欢的表情包 -->
    <div class="likeList" v-show="emjoyKind===1">
      暂未开发
    </div>
    <div class="emjoiClass">
      <div class="item tradition" @click="checkKind(0)">{{emjoyjson[0]}}</div>
      <div class="item like" @click="checkKind(1)"> <i class="iconfont">&#xe69d;</i> </div>
    </div>
  </div>
  <!-- 评论功能栏 -->
  <div class="editTab">
    <i class="iconfont" @click="showEmjoi()">&#xe670;</i>
    <i class="iconfont" @click="sorry()">&#xe716;</i>
    <span class="ps">最多可以编写40个字符哦({{ userComment.length}} / 40)</span>
  </div>
  <!-- 编辑评论 -->
  <textarea v-model="userComment"
  class="commentEdit" cols="30" rows="10">

  <div class="warnmessage">

  </div>
  </textarea>
  </Dialog>
</template>

<script>
import store from '@/store'
import Toptab from '@/components/Toptab.vue'
import allmessage from './components/allmessage.vue'
import mymessage from './components/mymessage.vue'
import avatar from '@/components/library/avatar.vue'
// 引用弹出框组件
import Dialog from '@/components/library/dialog.vue'
// 引入emjoi的Josn形态
import emjoyjson from './components/emjoy.json'
// 引入弹出消息提示用户
import popmessage from '@/components/library/message'
// 调用发送评论接口
import { addComment } from '@/api/comments'
export default {
  name: 'my-page',
  components: {
    Toptab,
    allmessage,
    mymessage,
    avatar,
    Dialog
  },
  data () {
    return {
      profile: null,
      // 展示全部消息还是我的消息
      actNum: 0,
      // 控制发言弹出框
      showDialog: false,
      // 储存用户编辑的言论
      userComment: '',
      // emjoy数据合集
      emjoyjson: '',
      // 控制表情包选择框展示与否
      EmjoiChoice: false,
      // 控制表情包种类
      emjoyKind: 0
    }
  },
  methods: {
    changeCheck (num) {
      this.$router.push({ query: {} })
      this.actNum = num
    },
    startComment () {
      this.showDialog = true
    },
    // 发布评论-----------------------
    confirm (show) {
      this.showDialog = show
      // 对输入内容做一个简单的判断 不能为空 包括全空格
      const content = this.userComment.split(' ').join('')
      if (content.length === 0) {
        console.log(content.length)
        popmessage({ type: 'error', str: '输入内容不合法' })
        this.userComment = ''
      } else {
        addComment({ comment: this.userComment }).then(res => {
          console.log(res.data)
          popmessage({ type: 'success', str: res.data.message })
          // 刷新组件的数据
          const allMsg = this.$refs.allMsg
          allMsg.refreshData()
          const myMsg = this.$refs.myMsg
          myMsg.refreshData()
        // console.log(myMsg, allMsg)
        })
      }
      // addComment({ comment: this.userComment }).then(res => {
      //   console.log(res.data)
      //   popmessage({ type: 'success', str: res.data.message })
      //   // 刷新组件的数据
      //   const allMsg = this.$refs.allMsg
      //   allMsg.refreshData()
      //   const myMsg = this.$refs.myMsg
      //   myMsg.refreshData()
      //   // console.log(myMsg, allMsg)
      // })
    },
    // 取消发布
    cancel (show) {
      this.userComment = ''
      this.showDialog = show
      // popmessage({ type: 'warn', str: '已取消评论' })
    },
    // sorry
    sorry () {
      this.EmjoiChoice = false
      popmessage({ type: 'warn', str: '抱歉该功能还未开发' })
    },
    // 展示表情包选择框
    showEmjoi () {
      this.EmjoiChoice = !this.EmjoiChoice
    },
    // 添加表情
    addEmjoy (emojoi) {
      this.userComment = this.userComment + emojoi
    },
    // 切换表情包种类
    checkKind (num) {
      this.emjoyKind = num
    }
  },
  watch: {
    // 监控输入字数
    userComment (New, Old) {
      if (New.length > 40) {
        popmessage({ type: 'warn', str: '已达到规定字数' })
        this.userComment = Old
      }
    }
  },
  created () {
    this.profile = store.state.user.profile
    this.emjoyjson = emjoyjson.data.split(',')
  }
}
</script>

<style scoped lang='less'>
.main{
  width: 1200px;
  margin:0 auto;
  min-height: 1200px;
  max-height: auto;
  background-color: white;
  border-left: 1px solid rgb(232, 229, 229);
  border-right: 1px solid rgb(232, 229, 229);
  .header{
    display: flex;
    height: 40px;
    line-height: 40px;
    .headeritem{
      color: rgb(129, 129, 129);
      border-radius: 10px 10px 0 0;
      cursor: pointer;
      margin-top: 10px;
      margin-left: 10px;
      margin-right: 10px;
      width: 100px;
      text-align: center;
    }
    .actitem{
      background-color: rgb(232, 229, 229);
      color: #3e87e5
    }
  }

  .body{
    min-height: 1600px;
    background-color: rgb(232, 229, 229);
  }
}
.body{
  display: flex;
  .usercard{
    position: relative;
    right: -5px;
    border-radius: 20px;
    margin-top: 20px;
    margin-left: 10px;
    margin-right: 10px;
    width: 300px;
    height: 300px;
    background-color: white;
    .user{
      display: flex;
      .avatar{
        margin-top: 10px;
        margin-left: 10px;
      }
      .userinfo{
        height: 100px;
        width: 180px;
        div{
          width: 200px;
          margin-left: 20px;
          margin-top: 20px;
          height: 30px;
          line-height: 30px;
          span{
            font-family: '钉钉进步体';
            white-space: nowrap;
            text-overflow: ellipsis;
            overflow: hidden;
          }
        }
      }
    }

    .submit{
      margin-top: 40px;
      width: 100%;
      height: 100px;
      .button{
        position: relative;
        top: 30px;
        background-color: #3e87e5;
        border: 1px solid #003f92;
        color: rgb(255, 255, 255);
        margin: 0 auto;
        cursor: pointer;
        width: 50%;
        height: 40px;
        line-height: 40px;
        text-align: center;
        border-radius: 20px;
        &:active{
          background-color: #003f92;
          color: rgb(231, 230, 230);
        }
      }
    }
  }
}

.commentEdit{
  padding: 50px;
  resize:none;
  border-radius: 20px;
  height: 180px;
  font-size: 20px;
  line-height: 50px;
  width: 100%;
  position: relative;
  top: 240px;
}
.editTab{
  border-bottom: 1px solid rgb(202, 202, 202);
  z-index: 1;
  height: 30px;
  line-height: 30px;
  width: 80%;
  position: absolute;
  right: 10%;
  top: 300px;
  .iconfont{
    cursor: pointer;
    margin-left: 40px;
    font-size: 24px;
    &:hover{
      color: #3e87e5;
    }
  }
  .ps{
    float: right;
    line-height: 30px;
    font-size: 10px;
    color: gray;
    font-family: '钉钉进步体';
  }
}
.emjoiBox{
  height: 200px;
  width: 400px;
  border: 1px solid gray;
  border-radius: 10px 10px 0 0;
  position: absolute;
  top: 100px;
  left: 10%;
  .ps{
    z-index: auto;
    position: absolute;
    left: 60px;
    font-size: 10px;
    color: gray;
  }
  .emjoiClass{
    border-top: 1px solid gray;
    position:absolute;
    right: 50px;
    bottom: 4px;
    display: flex;
    line-height: 30px;
    height: 30px;
    width: 300px;
    .item{
      width: 30px;
      height: 30px;
      cursor: pointer;
      margin-left: 10px;
      margin-right: 10px;
    }
    .like{
      color: red;
    }
  }
  //emjoi表情包列表
  .emjoiList{
    display: flex;
    flex-wrap: wrap;
    position: absolute;
    top: 10px;
    right: 50px;
    overflow: auto;
    height: 140px;
    max-height: 140px;
    width: 300px;

    .emjoi-item{
      text-align: center;
      cursor: pointer;
      margin-right: 10px;
      margin-left: 10px;
      margin-top: 10px;
      width: 20px;
      height: 20px;

      &:hover{
        font-size: 20px;
        line-height: 30px;
        position: relative;
        top: -5px;
        right: 5px;
      }
    }
  }

  .likeList{
    display: flex;
    flex-wrap: wrap;
    position: absolute;
    top: 10px;
    right: 50px;
    overflow: auto;
    height: 140px;
    max-height: 140px;
    width: 300px;
  }
}
</style>
